package com.quanquan.parking.controller;

import DTO.PageDTO;
import com.quanquan.parking.Service.ParkingService;
import com.quanquan.parking.dto.PayNotifyDTO;
import com.quanquan.parking.etcp.response.*;
import com.quanquan.parking.request.*;
import com.quanquan.parking.result.ResultETCPBody;
import com.quanquan.parking.utils.StringUtils;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import web.response.ResultBody;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @ClassName:ParkingController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/4/2 11:19
 * @Version 1.0
 */
@RestController
@RequestMapping("/parking")
public class ParkingController {


    private final ParkingService parkingService;

    @Autowired
    public ParkingController(ParkingService parkingService) {
        this.parkingService = parkingService;
    }

    @RequestMapping(value = "/get_token", method = RequestMethod.POST)
    public ResultBody getToken(String mobile) {
        String token = parkingService.getToken(mobile);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", token);
    }

    /**
     * 通过用户手机号查找绑定车牌
     *
     * @param request
     * @return
     */
    @PostMapping("/query_list")
    public ResultBody queryList(@RequestBody UserCarRequest request) {
        if (StringUtils.isEmpty(request.getMobile())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "手机号不可为空");
        }
        CarNumResponse carNumResponse = parkingService.findByMobile(request.getMobile());
        if (carNumResponse == null) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "未知错误");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", carNumResponse);
    }

  /*  *//**
     * 绑定车牌
     *
     * @param request
     * @return
     *//*
    @PostMapping("/bind_car")
    public ResultBody bindCar(@RequestBody UserCarRequest request) {
        if (StringUtils.isEmpty(request.getCarNumber())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "车牌号不可为空");
        }
        if (StringUtils.isEmpty(request.getMobile())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "手机号不可为空");
        }
        String msg = parkingService.bindCar(request.getCarNumber(), request.getMobile());
        if (msg.equals("success")) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "车牌绑定成功");
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL", msg);
    }*/

    /**
     * 解绑车牌
     *
     * @param request
     * @return
     */
    @PostMapping("/untying_car")
    public ResultBody untyingCar(@RequestBody UserCarRequest request) {
        if (StringUtils.isEmpty(request.getCarNumber())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "车牌号不可为空");
        }
        if (StringUtils.isEmpty(request.getMobile())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "手机号不可为空");
        }
        String msg = parkingService.untyingCar(request.getCarNumber(), request.getMobile());
        System.out.println(msg);
        if (msg == null) {
            return new ResultBody(ResCodeEnum.CUSTOM.getCode(), "未知错误");
        }
        if (msg.equals("success")) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "车牌解绑成功");
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), "请出场后解绑");
    }

    /**
     * 查询停车费
     *
     * @param request
     * @return
     */
    @PostMapping("/order_run_pay")
    public ResultBody orderunpay(@RequestBody UserCarRequest request) {
        if (StringUtils.isEmpty(request.getMobile())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "手机号不可为空");
        }
        if (StringUtils.isEmpty(request.getCarNumber())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "车牌不可为空");
        }
        if (request.getCarNumber().length() < 7) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "请输入正确的车牌号");
        }
        DataResponse response = parkingService.orderRunPay(request.getCarNumber(), request.getMobile());
        if (response.getCode() != 0) {
            return new ResultBody(ResCodeEnum.CUSTOM.getCode(), response.getMessage());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", response);
    }

    /**
     * 支付
     *
     * @param request
     * @return
     */
    @PostMapping("/order_pay")
    public ResultBody orderPay(@RequestBody UserCarRequest request) {
        if (StringUtils.isEmpty(request.getMobile())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "手机号不可为空");
        }
        if (StringUtils.isEmpty(request.getOrderId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "订单id不可为空");
        }
        DataResponse response = parkingService.orderPay(request.getMobile(), request.getOrderId());
        if (response.getCode()==0){
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", response);
        }
        return new ResultBody(ResCodeEnum.CUSTOM.getCode(), response.getMessage());
    }

    /**
     * 停车历史
     *
     * @param request
     * @return
     */
    @PostMapping("/order_history")
    public ResultBody orderHistory(@RequestBody UserCarRequest request) {
        if (StringUtils.isEmpty(request.getMobile())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "手机号不可为空");
        }
        List<OrderHistoryResponse> orderHistoryResponse = parkingService.orderHistory(request.getMobile());
        if (orderHistoryResponse.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无停车记录");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", orderHistoryResponse);
    }

    /**
     * 停车场状态
     *
     * @return
     */
    @PostMapping("/parking_status")
    public ResultBody parkingStatus() {
        ParkingResponse parkingResponse = parkingService.parkingStatus();
        if (parkingResponse != null) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", parkingResponse);
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
    }

    /**
     * 主动支付结果通知
     *
     * @return
     */
    @PostMapping("/payNotify")
    public ResultETCPBody payNotify(@RequestBody PayNotifyRequest request) {
        boolean flag = parkingService.payNotify(request);
        if (flag) {
            return new ResultETCPBody(0, "ok");
        }
        return new ResultETCPBody(-1, "Fail");
    }

    /**
     * 车辆出场通知
     *
     * @return
     */
    @PostMapping("/outNotify")
    public ResultETCPBody outNotify(@RequestBody OutNotifyRequest request) {
        boolean flag = parkingService.outNotify(request);
        if (flag) {
            return new ResultETCPBody(0, "ok");
        }
        return new ResultETCPBody(-1, "Fail");
    }

    /**
     * 车辆入场通知
     *
     * @return
     */
    @PostMapping("/inNotify")
    public ResultETCPBody inNotify(@RequestBody InNotifyRequest request) {
        boolean flag = parkingService.inNotify(request);
        if (flag) {
            return new ResultETCPBody(0, "ok");
        }
        return new ResultETCPBody(-1, "Fail");
    }

    /**
     * 车辆进出场模拟
     *
     * @param request
     */
    @PostMapping("/simulation")
    public ResultBody simulation(@RequestBody UserCarRequest request) {
        if (StringUtils.isEmpty(request.getCarNumber())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "车牌不可为空");
        }
        SimulationResponse simulationResponse = parkingService.simulation(request.getCarNumber());
        if (simulationResponse != null) {
            System.out.println(simulationResponse);
            if (simulationResponse.getIsInpark() == 0) {
                return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "车辆出厂");
            }
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "车辆入厂");
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
    }

    @PostMapping("/tt")
    public void tt(String mobile, String orderId) {
        parkingService.tt(mobile, orderId);
    }

    @PostMapping("/simulation1")
    public void simulation1(String plateNum) {
        parkingService.simulation1(plateNum);
    }

    @PostMapping("/orderRunPay1")
    public void orderRunPay1(String plateNum, String mobile) {
        parkingService.orderRunPay1(plateNum, mobile);
    }

    @PostMapping("/bindCar1")
    public void bindCar1(String plateNum, String mobile) {
        parkingService.bindCar1(plateNum, mobile);
    }

    @PostMapping("/carnum")
    public void carnum(String mobile) {
        parkingService.carnum(mobile);
    }

    @PostMapping("/orderPay1")
    public void orderPay1(String mobile, String orderId) {
        parkingService.orderPay1(mobile, orderId);
    }

    @PostMapping("/couponList")
    public void couponList() throws UnsupportedEncodingException {
        parkingService.couponList();
    }

    @PostMapping("/record")
    public ResultBody record(@RequestBody SendCouponRequest request) throws UnsupportedEncodingException {
        String carNumber = request.getCarNumber();
        String couponFreeId = request.getCouponFreeId();
        String flag = parkingService.record(carNumber, couponFreeId);
        if (flag != null) {
            if (flag.equals("1")) {
                return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS");
            }
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
    }

    @PostMapping("/parking_record")
    public ResultBody parkingRecord(@RequestBody PayNotifyRequest request) {
        PageDTO<PayNotifyDTO> payNotifyDTOPageDTO = parkingService.parkingRecord(request);
        if (null == payNotifyDTOPageDTO) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", payNotifyDTOPageDTO);
    }



}
